package com.views
{
	import com.GameModel;
	import com.config.Level;
	import com.controllers.GameController;
	import com.datas.vo.ItemVO;
	import com.views.objects.GameItemView;
	
	import flash.display.Sprite;
	import flash.utils.Dictionary;

	public class GamePlayLayerView extends Sprite
	{
		private var _ctrl:GameController;
		private var _itemViewList:Dictionary;
		private var _idCount:int;
		private var _gameModel:GameModel;
		private var _dic:Dictionary = new Dictionary;
		
		public function GamePlayLayerView(p_ctrl:GameController)
		{
			super();
			_itemViewList = new Dictionary
			_ctrl = p_ctrl;
			_gameModel = GameModel.getInstance();
		}
		public function initView():void{//创建显示数组
			var row:int = Level.ROW;
			var col:int = Level.COL;
			for(var i:int = 0;i<row;i++){
				for(var j:int = 0;j<col;j++){
					var itemView:GameItemView = new GameItemView(_ctrl);
					itemView.move(i*35,j*35);
					this.addChild(itemView);
					var tempIndex:String = String(i)+"_"+String(j)
					_itemViewList[tempIndex] = itemView;
					_idCount++;
				}
			}
			_gameModel.idCount = _idCount;
		}
		public function stopCreateRandomView():void{
						//更新ID 
			for each(var item:GameItemView in _itemViewList){
				if(item.itemVO){
					if(item.itemVO.id == 2){
						item.itemVO.id = 1
					}
				}
			}
		}
		public function createView():void{
			var item:Array = _gameModel.itemList;
			for(var i:int = 0;i<item.length;i++){
				for(var j:int = 0;j<item[0].length;j++){
					if(item[i][j].id == 1){
						var temp:String = String(item[i][j].row)+"_"+String(item[i][j].col); 
						_itemViewList[temp].itemVO = item[i][j]
						_itemViewList[temp].show();
						_dic[temp] = temp;
					}
				}
			}
			_gameModel.itemVODic = _dic
		}
		
		public function updatePlayLayerView(p_vo1:ItemVO,p_vo2:ItemVO):void{
			var temp:String = String(p_vo1.row)+"_"+String(p_vo1.col);
			_itemViewList[temp].itemVO = p_vo1;
			_itemViewList[temp].show();
			temp = String(p_vo2.row)+"_"+String(p_vo2.col);
			_itemViewList[temp].itemVO = p_vo2;
			_itemViewList[temp].show();
		}
		
		
		public function removeItem( $item:GameItemView ):void {
			$item.destroy( );
		}
		
		public function getItemByVO( itemVO:ItemVO ):GameItemView {
			for each(var item:GameItemView in _itemViewList){
				if(item.itemVO == itemVO){
					return item
				}
			}
			return null
		}
		
	}
}